package cn.oldsix.spark.api;

import cn.oldsix.spark.streaming.vo.UserBehaviour;
import cn.oldsix.spark.streaming.vo.UserBehaviourDetail;
import com.alibaba.fastjson.JSON;
import org.apache.http.HttpEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;

import java.net.URLEncoder;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Random;

/*
 * @Author : Wu.D.J
 * @Create : 2018.09.13
 **/


public class UserBehaviourApi {

    public static String[] packages = {
            "com.eg.android.AlipayGphone", "com.taobao.taobao",
            "com.tencent.mobileqq", "com.tencent.mm", "com.qiyi.video",
            "com.jingdong.app.mall", "com.tencent.qqmusic",
            "com.achievo.vipshop", "com.mt.mtxx.mtxx", "com.youku.phone",
            "com.baidu.searchbox", "com.tencent.qqlive", "com.tmall.wireless",
            "com.dianping.v1", "com.sina.weibo", "me.ele",
            "com.sdu.didi.psnger", "com.UCMobile",
            "com.smile.gifmaker", "com.ss.android.article.news"
    };

    public static Long[] users = {
            1000000L, 2000000L, 3000000L,
            4000000L, 5000000L, 6000000L,
            7000000L, 8000000L, 9000000L
    };

    public static String mockData(Long userId, Long spaceTime) {
        UserBehaviour behaviour = new UserBehaviour();
        String day = new SimpleDateFormat("yyyy-MM-dd").format(new Date());
        long beginTime = System.currentTimeMillis();
        long endTime = beginTime + spaceTime;
        behaviour.setUserId(userId);
        behaviour.setDay(day);
        behaviour.setBeginTime(beginTime);
        behaviour.setEndTime(endTime);
        List<UserBehaviourDetail> list = new ArrayList<>();
        UserBehaviourDetail detail1 = new UserBehaviourDetail();
        int index = new Random().nextInt(20);
        String packageName = packages[index];
        detail1.setPackageName(packageName);
        detail1.setActiveTime("300000");
        list.add(detail1);
        int index2 = 0;
        while (true) {
            index2 = new Random().nextInt(20);
            if (index2 != index) {
                break;
            }
        }
        UserBehaviourDetail detail2 = new UserBehaviourDetail();
        String packageName2 = packages[index2];
        detail2.setPackageName(packageName2);
        detail2.setActiveTime("300000");
        list.add(detail2);

        behaviour.setData(list);

        return JSON.toJSONString(behaviour);
    }

    public static void main(String[] args) throws InterruptedException {
        Long spaceTime = 12000L;
        String url = "http://192.168.216.128:8081/upload?json=";
        while (true) {
            long start = System.currentTimeMillis();
            for (int i = 0; i < users.length; i++) {
                String result = mockData(users[i], spaceTime);
                System.out.println(result);
                try {
                    HttpGet httpGet = new HttpGet(url + URLEncoder.encode(result, "UTF-8"));
                    CloseableHttpClient httpClient = HttpClients.createDefault();
                    CloseableHttpResponse response = httpClient.execute(httpGet);
                    HttpEntity entity = response.getEntity();
                    String responseBody = EntityUtils.toString(entity);
                    System.out.println(responseBody);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
            long end = System.currentTimeMillis();
            long space = end - start;
            Thread.sleep(spaceTime - space);
        }
    }
}
